package com.yuand.config;

import com.yuand.interceptor.JwtInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.*;

/**
 * @author yuandong
 * @date 2021/9/9 11:42
 * 拦截器 资源映射
 */
@Configuration
public class ResourceConfig implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry){
        /** 本地文件上传路径 */
        registry.addResourceHandler( "/profile/**").addResourceLocations("file:" + Global.getProfile() + "/");
    }



    //配置跨域请求
    public void addCorsMappings(CorsRegistry  registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedHeaders("*")
                .allowedMethods("*");
    }

    public void addInterceptors(InterceptorRegistry registry) {
        InterceptorRegistration interceptorRegistration = registry.addInterceptor(jwtInterceptor());
        // 需拦截的路径
        interceptorRegistration.addPathPatterns("/**");
        // 需放行的路径(profile 请求静态资源时放行)
        interceptorRegistration.excludePathPatterns("/**/login", "/**/info","/**/sys/**", "/**/unlogin", "/**/profile/**","/**/druid/**");
    }


    //以这种方式将拦截器注入为一个bean，可以防止拦截器中无法注入bean的问题出现
    @Bean
    public JwtInterceptor jwtInterceptor(){
        return new JwtInterceptor();
    }

    /**
     * 除了使用@Bean的方式进行注入，还可以使用
     * @Autowired
     * private JwtInterceptor jwtInterceptor; 进行注入，前提是在该类的上面加入@Component注解
     */



}
